package cn.micai.base.lambda;

import java.util.ArrayList;
import java.util.List;

/**
 * 描述：Java 8 forEach examples forEach and List
 * <p>
 *
 * @author: 赵新国
 * @date: 2018/6/21 15:01
 */
public class ForEachListExample {

    public static void main(String [] args) {

        List<String> items = new ArrayList<>();

        items.add("A");
        items.add("B");
        items.add("C");
        items.add("D");
        items.add("E");
        items.add("F");

        for (String item : items) {
            System.out.println(item);
        }

        System.out.println("--------------------------------------------");

        // In Java 8, you can loop a List with forEach + lambda expression or method reference.

        // lambda
        // Output : A,B,C,D,E,F
        items.forEach(item->System.out.println(item));

        System.out.println("--------------------------------------------");

        // Output : C
        items.forEach(item->{
            if ("C".equals(item)) {
                System.out.println(item);
            }
        });

        System.out.println("--------------------------------------------");

        // method reference
        // Output : A,B,C,D,E,F
        items.forEach(System.out::println);

        System.out.println("--------------------------------------------");

        // Stream and filter
        // Output : B
        items.stream()
                .filter(s->s.contains("B"))
                .forEach(System.out::println);

    }
}
